using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace VisualEsse.DataEngine
{
    public abstract class EsseResponseDataProvider : DataProvider
    {
        public EsseResponseDataProvider(string homeDirectory)
            : base(homeDirectory)
        {
        }

        public EsseResponseDataProvider(string homeDirectory, bool createDefaultMetadataProvider)
            : base(homeDirectory, createDefaultMetadataProvider)
        {
        }


        public override void Dispose()
        {
            base.Dispose();
        }

        public static DataProvider Create(string fileName, string homeDirectory)
        {
            string error  = null;
            try
            {
                NcmlDataProvider ncml = new NcmlDataProvider(fileName, homeDirectory);
                return ncml;
            }
            catch(Exception ex)
            {
                error = ex.Message;
            }

            try
            {
                EssePackDataProvider ep = new EssePackDataProvider(fileName, homeDirectory);
                return ep;
            }
            catch (Exception ex2)
            {
                if (error != null)
                    error += " -or- " + ex2.Message;
                else
                    error = ex2.Message;
            }

            throw new Exception(error);
        }

        public static DataProvider Create(Stream stream, string homeDirectory)
        {
            string error = null;
            XmlReader reader = XmlReader.Create(stream);
            try
            {
                NcmlGeoGridDocument doc = new NcmlGeoGridDocument();
                doc.Load(reader);

                NcmlDataProvider ncml = new NcmlDataProvider(doc, homeDirectory);
                return ncml;
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            finally 
            {
                reader.Close();
            }

            throw new Exception(error);
        }
    }

}
